package jjn.carl.dp;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Jiang Jining
 * @since 2023-08-20 22:59
 */
public class LeetCode279 {
    
    public int numSquares(int n) {
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        int max = (int) Math.sqrt(n);
        for (int i = 1; i <= max; i++) {
            for (int j = i * i; j <= n; j++) {
                dp[j] = Math.min(dp[j], dp[j - i * i] + 1);
            }
        }
        return dp[n];
    }
    
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int numSquares = new LeetCode279().numSquares(n);
        System.out.println(numSquares);
    }
}
